<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/05 19:58:31 $)
// $RCSfile: impex.knowledgebase.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Return the Total Pages for this Plugin
*/
function getPluginTotalPages3()
{
	return 3;
}

/**
* Run the specific page for plugin
*/
function runPluginPage3($page)
{
	global $impexDB, $_IMPEXHASH;

	// Import Categories
	if ($page == 1)
	{
		$categorylist = array();

		// First fetch old categories
		$impexDB->query("SELECT * FROM `perlDesk_kb_cat` ORDER BY `id` ASC;");
		while ($impexDB->nextRecord())
		{
			$categorylist[$impexDB->Record["id"]] = $impexDB->Record;
		}

		// Now insert new categories
		$displayorder = 1;
		foreach ($categorylist as $key=>$val)
		{
			// Parent is set to 0 for now
			$_kbcategoryid = _impexInsertKBCategory(0, $val["category"], "0", $displayorder);
			$_IMPEXHASH["kbcatpointer"][$key] = $_kbcategoryid;

			displayStatus("Imported KB Category: ".htmlspecialchars($val["category"]));
			$displayorder++;
		}

		// Ok. Now that all categories are imported, we need to create a tree based on old data
		foreach ($categorylist as $key=>$val)
		{
			$oldcategoryid = $key;
			$newkbcategoryid = intval($_IMPEXHASH["kbcatpointer"][$key]);
			$newparentcategoryid = intval($_IMPEXHASH["kbcatpointer"][$val["parent_category"]]);

			if ($newkbcategoryid == "")
			{
				displayStatus("<font color='red'>WARNING:</font> KB Category Import Failed. Invalid Pointer. $key ($newkbcategoryid - $newparentcategoryid): ".$val["parent_category"]);
			} else {
				_impexUpdateKBCategoryParent($newkbcategoryid, $newparentcategoryid);

				displayStatus("Updated Tree for KB Category: ".htmlspecialchars($val["category"]));
			}
		}


	// Import Articles
	} else if ($page == 2) {
		$articlelist = array();

		// Fetch old questions
		$impexDB->query("SELECT * FROM `perlDesk_kb_entries` ORDER BY id ASC;");
		while ($impexDB->nextRecord())
		{
			$articlelist[$impexDB->Record["id"]] = $impexDB->Record;
		}

		// Now insert the new kb articles
		$displayorder = 1;
		foreach ($articlelist as $key=>$val)
		{
			$_kbarticleid = _impexInsertKBArticle($val["subject"], $val["description"], $val["views"], $displayorder, DATENOW);

			$_IMPEXHASH["kbarticlepointer"][$key] = $_kbarticleid;

			displayStatus("Imported KB Article: ".htmlspecialchars($val["subject"]));
			$displayorder++;
		}


	// Insert Article <> Category Links
	} else if ($page == 3) {
		$articlelist = array();

		// Fetch old questions
		$impexDB->query("SELECT `id`, `catid` FROM `perlDesk_kb_entries` ORDER BY `id` ASC;");
		while ($impexDB->nextRecord())
		{
			$articlelist[$impexDB->Record["id"]] = $impexDB->Record["catid"];
		}

		// Insert the Links
		foreach ($articlelist as $key=>$val)
		{
			$newkbcategoryid = $_IMPEXHASH["kbcatpointer"][$val];
			$newkbarticleid = $_IMPEXHASH["kbarticlepointer"][$key];

			if ($newkbcategoryid == "" || $newkbarticleid == "")
			{
				displayStatus("<font color='red'>WARNING:</font> Unable to Import Article Link. Invalid KB Category ID: $val");
			}

			_impexInsertKBArticleLink($newkbcategoryid, $newkbarticleid);

			displayStatus("Imported Article Link for Category (ID: ". intval($newkbcategoryid) .") and Article (ID: ". intval($newkbarticleid) .")");
		}



	}
}

?>